package com.tcl.launcherpro.lockscreen;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.net.core.service.config.ServiceRemoteConfigInstance;
import com.net.core.unit.ServicesConfigurationConstant;
import com.tcl.launcherpro.lockscreen.statistics.LockReportManager;
import com.tcl.launcherpro.lockscreen.statistics.LockStatisticsEventConst;
import com.tcl.launcherpro.lockscreen.utils.BatteryInfo;

import java.util.Calendar;

/**
 * <br>类描述：监听锁屏事件以及是否充电事件的Receiver
 * <br>详细描述：
 * <br><b>Author sichard</b>
 * <br><b>Date 2017/3/6</b>
 */
public class LockScreenReceiver extends BroadcastReceiver {
    /**
     * <br>功能简述:是否正在充电
     *
     * @return
     */
    private boolean isCharging(Context context) {
        return BatteryInfo.isElectricity(context);
    }

    public static int COUNT_DAILY = 0;

    private static final String TAG = "LockScreenReceiver";

    @Override
    public void onReceive(final Context context, Intent intent) {
        final LockScreenManager lockScreenManager = LockScreenManager.getInstance(context);


        String isShowLockScreenFromServerStr = ServiceRemoteConfigInstance.getInstance(context).getString(ServicesConfigurationConstant.IS_SHOW_LOCK_SCREEN);
        boolean isShowLockScreenFromServer = false;
        isShowLockScreenFromServer = Boolean.parseBoolean(isShowLockScreenFromServerStr);

        if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){
            Log.d("wxj", "onReceive: LockScreenReceiver");
            COUNT_DAILY ++;
            Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            LockReportManager.getInstance().onEvent(LockStatisticsEventConst.LAUNCHER_LOCK_CONNECT,""+hour);
        }


        if (isShowLockScreenFromServer && (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_POWER_CONNECTED))) {
            boolean isShowLockScreen = lockScreenManager.isShowLockScreen();
            if (isShowLockScreen && isCharging(context)) {
                // 隐式启动锁屏页
                final Intent intent1 = new Intent("android.intent.action.LOCK_SCREEN");
//			Intent intent1 = new Intent(context, LockScreenActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent1.putExtra(LockScreenActivity.START_LEVEL, BatteryInfo.getLevelPercent(context));
                context.startActivity(intent1);
            }
        }
    }
}
